/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package agent;

import robosym.utils.Geo;
import robosym.visual.Analyzer;
import robosym.visual.Barrier;
import robosym.visual.emulator.AnalyzerEmulator;

/**
 *
 * @author TR
 */
public class Agent {
    
    public static float SPEED = 3;
  
    float x;
    float y;
    
    float angleRad = 0;
    
    //Visual analyzer
    AnalyzerEmulator analyzer = new AnalyzerEmulator();
    
    //cords when agent muxt to go
    float targetX;
    float targetY;
    
    //tamplary cords that presents vector for traveling
    float travelX;
    float travelY;
    
    protected void dispatchOwnPostition(){
        analyzer.setPosition(x, y, angleRad);
    }

    public Agent(float x, float y) {
        this.x = x;
        this.y = y;
        targetX = x;
        targetY = y;
        dispatchOwnPostition();
    }
    
    /**
     * this method presents actions of agent on the time 
     * interval
     */
    public void tick(){
        calculateTravel();
        if(Geo.distance(travelX, travelY) < SPEED){
            x = targetX;
            y = targetY;
        }else{
            
            stepToTarget();
        }
        dispatchOwnPostition();
    }    
    
    protected void calculateTravel(){
        travelX = targetX - x;
        travelY = targetY - y;
    }
    
    protected void calculateAngleRad(){
        angleRad = Geo.angleOfVectorRad(travelX, travelY);
    }
    
    protected void stepToTarget(){

        calculateAngleRad();
        
        float pathX = SPEED * (float)Math.cos(angleRad);
        float pathY = SPEED * (float)Math.sin(angleRad);
        
        x+=pathX;
        y+=pathY;
    }

    public void gotoXY(float targetX, float targetY) {
        this.targetX = targetX;
        this.targetY = targetY;
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }
    
    /**
     * get seen barirers from agent
     */ 
    public Barrier[] getSightBarriers() {
        return analyzer.getSightBarriers();
    }
    
}
